/**
 * Created with IntelliJ IDEA.
 * User: duoba
 * Date: 16/2/24
 * Time: 下午1:39
 * To change this template use File | Settings | File Templates.
 */

package com.cake21.uitest.System.Core;

import java.io.File;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import com.cake21.uitest.System.Map.CaseMap;
import org.openqa.selenium.WebDriver;

public abstract class Case {
    /*用例名*/
    protected String name = "";
    /*用例描述*/
    protected String description = "";
    /*浏览器对象实例*/
    protected Browser browser = initBrowser();

    /*初始化浏览器对象实例*/
    protected Browser initBrowser() {
        return Server.getCommander().getThreadMap().get(Thread.currentThread().getId());
    }

    /*浏览器测试判断*/
    protected Judge judge = initJudge();

    /*初始化浏览器测试判断*/
    protected Judge initJudge() {
        return browser.getJudge();
    }

    /*页面超时时间(s)*/
    protected Integer pageLoadTimeout = 30;

    /*初始化页面超时时间*/
    protected Integer initPageLoadTimeout(String key) {
        Integer pageLoadTimeout = browser.getPageLoadTimeout();
        try {
            // 读取配置错误或无配置时，使用浏览器级别配置
            String pageLoadValue = CaseMap.getMap().get(key).get("pageLoadTimeout");
            // 覆盖浏览器级别配置
            pageLoadTimeout = (!pageLoadValue.equals("")) ? Integer.parseInt(pageLoadValue) : browser.getPageLoadTimeout();
        } catch (Exception e) {
            // 配置设置错误
            e.printStackTrace();
        }
        return pageLoadTimeout;
    }

    /*脚本超时时间(s)*/
    protected Integer scriptTimeout = 30;

    /*初始化脚本超时时间*/
    protected Integer initScriptTimeout(String key) {
        Integer scriptTimeout = browser.getScriptTimeout();
        try {
            // 读取配置错误或无配置时，使用浏览器级别配置
            String scriptValue = CaseMap.getMap().get(key).get("scriptTimeout");
            // 覆盖浏览器级别配置
            scriptTimeout = (!scriptValue.equals("")) ? Integer.parseInt(scriptValue) : browser.getScriptTimeout();
        } catch (Exception e) {
            // 配置设置错误
            e.printStackTrace();
        }
        return scriptTimeout;
    }

    /*等待超时时间(s)*/
    protected Integer implicitlyWait = 10;

    /*初始化等待超时时间*/
    protected Integer initImplicitlyWait(String key) {
        Integer implicitlyWait = browser.getImplicitlyWait();
        try {
            // 读取配置错误或无配置时，使用浏览器级别配置
            String implicitnessValue = CaseMap.getMap().get(key).get("implicitlyWait");
            // 覆盖浏览器级别配置
            implicitlyWait = (!implicitnessValue.equals("")) ? Integer.parseInt(implicitnessValue) : browser.getImplicitlyWait();
        } catch (Exception e) {
            // 配置设置错误
            e.printStackTrace();
        }
        return implicitlyWait;
    }

    /*初始化用例信息*/
    public Case initial(String key) {
        /*根据索引获取用例名，用例描述*/
        name = CaseMap.getMap().get(key).get("name");
        description = CaseMap.getMap().get(key).get("description");
        pageLoadTimeout = initPageLoadTimeout(key);
        scriptTimeout = initScriptTimeout(key);
        implicitlyWait = initImplicitlyWait(key);
        return this;
    }

    /*用例测试执行*/
    public void launch() {
        judge.info("测试用例: " + name + " >> 开始执行");
        browser.getDriver().manage().timeouts().pageLoadTimeout(pageLoadTimeout, TimeUnit.SECONDS);
        browser.getDriver().manage().timeouts().setScriptTimeout(scriptTimeout, TimeUnit.SECONDS);
        browser.getDriver().manage().timeouts().implicitlyWait(implicitlyWait, TimeUnit.SECONDS);
        before();
        /*用例需要继承Case类并实现execute方法*/
        execute();
        after();
        judge.info("测试用例: " + name + " >> 结束");
    }

    /*用例主体执行代码*/
    protected abstract void execute();

    /*用例前置代码*/
    protected void before() {
        /*基类不做任何事*/
    }

    /*用例后置代码*/
    protected void after() {
        /*基类不做任何事*/
    }

    /*截屏*/
    public String screenshot() {
        String path = Server.getCommander().getNamex() + "/" + Server.generateId() + ".png";
        try {
            Utils.GetReadyForFullPageScreenShot(browser.getDriver());
            File source = ((TakesScreenshot) browser.getDriver()).getScreenshotAs(OutputType.FILE);
            File target = new File("report/" + path);
			/*快照图片存放于report/{测试标识号}/目录下*/
            FileUtils.copyFile(source, target);
        } catch (Exception e) {
            /*截屏错误*/
            e.printStackTrace();
        }
        return path;
    }

    /*截屏2,传入WebDriver实例*/
    public String screenshot(WebDriver driver) {
        String path = Server.getCommander().getNamex() + "/" + Server.generateId() + ".png";
        try {
            Utils.GetReadyForFullPageScreenShot(driver);
            File source = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            File target = new File("report/" + path);
            /*快照图片存放于report/{测试标识号}/目录下*/
            FileUtils.copyFile(source, target);
        } catch (Exception e) {
            /*截屏错误*/
            e.printStackTrace();
        }
        return path;
    }
}